<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/5. zabbix2.2 使用自定义脚本监控网卡流量/">zabbix2.2 使用自定义脚本监控网卡流量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<ol>
<li>在客户端修改配置文件  /etc/zabbix/zabbix_agentd.conf</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要改动两个地方：</p>
<ul>
<li>UnsafeUserParameters=1</li>
<li>UserParameter=my.net.if</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/usr/local/sbin/zabbix/net.sh $1 $2   //其中UserParameter用来自定义键值，（类似于net.if.in)，自己写的脚本往往会有参数，所以需要加。这是固定写法，如果脚本压根就没有什么参数，那么这个就省了。逗号后面就是我们写的脚本的路径了，再后面就是要用到的参数，有几个就写几。</p>
<ol>
<li>编写脚本</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi   /usr/<span class="built_in">local</span>/sbin/zabbix/net.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">eth=<span class="variable">$1</span></div><div class="line">io=<span class="variable">$2</span></div><div class="line">net_file=<span class="string">"/proc/net/dev"</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$2</span> == <span class="string">"in"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    n_new=`grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $2&#125;'</span>`</div><div class="line">    n_old=`tail -1 /tmp/neti.log`</div><div class="line">    n=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n_new</span>-<span class="variable">$n_old</span>"</span>|bc`</div><div class="line">    d_new=`date +%s`</div><div class="line">    d_old=`tail -2 /tmp/neti.log|head -1`</div><div class="line">    d=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$d_new</span>-<span class="variable">$d_old</span>"</span>|bc`</div><div class="line">    if_net=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n</span>/<span class="variable">$d</span>"</span>|bc`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$if_net</span></div><div class="line">    date +%s&gt;&gt;/tmp/neti.log</div><div class="line">    grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $2&#125;'</span>&gt;&gt;/tmp/neti.log</div><div class="line"><span class="keyword">elif</span> [ <span class="variable">$2</span> == <span class="string">"out"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    n_new=`grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $10&#125;'</span>`</div><div class="line">    n_old=`tail -1 /tmp/neto.log`</div><div class="line">    n=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n_new</span>-<span class="variable">$n_old</span>"</span>|bc`</div><div class="line">    d_new=`date +%s`</div><div class="line">    d_old=`tail -2 /tmp/neto.log|head -1`</div><div class="line">    d=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$d_new</span>-<span class="variable">$d_old</span>"</span>|bc`</div><div class="line">    if_net=`<span class="built_in">echo</span> <span class="string">"<span class="variable">$n</span>/<span class="variable">$d</span>"</span>|bc`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$if_net</span></div><div class="line">    date +%s&gt;&gt;/tmp/neto.log</div><div class="line">    grep <span class="string">"<span class="variable">$eth</span>"</span> <span class="variable">$net_file</span>|awk <span class="string">'&#123;print $10&#125;'</span>&gt;&gt;/tmp/neto.log</div><div class="line"><span class="keyword">else</span></div><div class="line">    <span class="built_in">echo</span> 0</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本的思路，就是通过查看文件 /proc/net/dev  里面的数值，来计算实时网卡流量，其实我算的是一个平均值。分为进和出。如果这个脚本每隔1分钟执行一次，那么算出来的流量值就是1分钟的平均值。</p>
<ol>
<li>在脚本执行前，需要先做一个操作</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">touch /tmp/net[io].<span class="built_in">log</span></div><div class="line">date +%s &gt;&gt;/tmp/neti.log</div><div class="line">grep eth0 /proc/net/dev |awk <span class="string">'&#123;print $2&#125;'</span> &gt;&gt;/tmp/neti.log</div><div class="line">date +%s &gt;&gt;/tmp/neto.log</div><div class="line">grep eth0 /proc/net/dev |awk <span class="string">'&#123;print $10&#125;'</span> &gt;&gt;/tmp/neto.log</div><div class="line">chown zabbix  /tmp/net[io].<span class="built_in">log</span></div></pre></td></tr></table></figure>
<ol>
<li>检查脚本是否可用</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在服务端执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">zabbix_get -s 192.168.31.166 -p10050 -k <span class="string">"my.net.if[eth0,out]"</span></div></pre></td></tr></table></figure>
<ol>
<li>如果可以返回数值说明没问题了，接着在浏览器里面配置</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;组态 –&gt; 主机 –&gt; 项目  –&gt; 创建监控项</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;名称 “网卡流量出”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类型默认“zabbix代理”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;键值  “my.net.if[eth0,out]”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;数据更新间隔  60</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;存档</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/4. Zabbix自定义监控脚本配置/">Zabbix自定义监控脚本配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：要添加自定义脚本监控，必须升级zabbix agent版本至2.0.0以上，这里以2.0.8为例</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置方式：</p>
<ol>
<li>完成自定义监控脚本的编写（windows或linux脚本）</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本要求：</p>
<ul>
<li>既然是监控，那必然要有输出结果值（字符串，数字皆可） </li>
<li>必须要求zabbix用户有执行权限，当然可以直接设置所有用户都有执行权限（chmod 777 脚本文件）</li>
<li>若脚本需要传入参数，按照参数传入的顺序，在脚本中可用$1-$9来引用传入的参数</li>
</ul>
<ol>
<li>找到zabbix agent的配置文件zabbix_agentd.conf,修改如下两个参数</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">UnsafeUserParameters=0 =&gt; UnsafeUserParameters=1  <span class="comment">#并去掉前面的注释符</span></div><div class="line">UserParameter= =&gt; UserParameter=aaa.bbb[*], /usr/<span class="built_in">local</span>/script/monitor.sh <span class="variable">$1</span> <span class="variable">$2</span> ...</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：aaa.bbb[*] —zabbix服务器添加监控信息时需要用到的key值，</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;格式：<br>aaa.bbb<a href="例：system.file.size[*]">*</a> /usr/local/script/monitor.sh —-监控脚本绝对路径</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了便于灵活监控，有时脚本需要传入参数，此参数可从zabbix服务器端传入，所有参数按顺序分别从$1-$9表示</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：</p>
<ul>
<li>若无需传入参数，则红色部分可省略</li>
<li>该自定义脚本可由zabbix服务器控制收集数据的频率（如：每30s运行一次），无需再添加计划任务</li>
<li>以上参数请根据实际情况填写，并注意去除参数前注释符(#)</li>
<li>注意在key值和后面的脚本之间有个逗号隔开</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;至此，自定义监控脚本zabbix agent端配置结束</p>
<ol>
<li>测试</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;测试命令: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/bin/zabbix_agentd -t key[参数]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;示例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/bin/zabbix_agentd -t system.file.size[/etc/a.txt,abc,...]</div></pre></td></tr></table></figure>
<ol>
<li>监控脚本举例：</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本名称：/usr/local/script/monitor.sh</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本内容：echo <code>date +&quot;%F %T&quot;</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本要求：必须在控制台输出值，该值将作为返回值返回给zabbix服务器端</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/3. zabbix 增加客户端监控/">zabbix 增加客户端监控</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix 比 nagios 好的地方就是， zabbix 不需要命令行去配置，只需要在浏览器里面点点按钮就可以了，这样就直观和方便很多。在配置客户端之前，也需要在客户端上安装 zabbix。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y zabbix20-agent</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后更改客户端机器上的 zabbix_agentd.conf 配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/zabbix_agentd.conf</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为 服务端 ip</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/04.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为（自定义，但必须唯一）</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%20%E5%A2%9E%E5%8A%A0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9B%91%E6%8E%A7/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动客户端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/zabbix-agent start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端上命令行测试</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@zabbix ~]<span class="comment"># zabbix_get -s 192.168.0.98 -p10050 -k "system.hostname"</span></div><div class="line">lnmp</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在 web 界面下，点 configura（配置） –&gt; host（主机）  右上角点 create host （创建主机）其中 host name（主机名称） ，visible name（可见的名称） 自定义 ，可以选择 groups（组），这里默认即可， ip address（ip地址）写入客户端 ip</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还可以配置监控项目模版： 点 “templates”（模版） ，点 add （添加），在弹出的小窗口中选择 Template OS Linux ，然后点 select（选择），最后点 save（存档） ，这些模块肯定是不能满足需求的，所以可以自定义监控模版。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix 自带了很多模版，模版中有很多监控项目，比如 CPU、网卡、内存、进程等等。使用系统自带模版有点太多了，所以可以自定义模版。点 configuration（配置） 选择 templates（模版） ，点右上角的 cteate template（创建模版）。 Template name（模版名称） 和 Visible name（可见的名称） 自定义，Groups（组） 选择 templates ，点 save存档 。然后去挑选一些项目拷贝到该模版下：比如找到 Templates OS Linux 点一下 items（项目） ，选择想要的项目，然后在下面选择 copy selected to …（复制所选的到…） 然后点 go（确认） 。Group 选择 templates ，找到刚才自定义的 templates ， 点 copy 。然后点 configuration（配置）选择 templates 可以看到新建的 templates 中已经有刚刚 copy 的 items 了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以使用和上面相同的方法自定义拷贝 Triggers（触发器），它用来设置告警的阀值，当然也可以自定义编辑它</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/26. zabbix自动发现实现批量监控docker状态/">zabbix自动发现实现批量监控docker状态</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最近在搞zabbix监控docker以及docker内部应用状态信息，网上找的资料好少，只找到了一个大神的一篇文章，用的是python实现监控docker容器的基本状态，在他给的脚本基础上进行修改，另外，增加了docker内部常用应用的状态监控，目前在测试环境上部署成功了，具体还需要在线上环境部署后才能检验出效果如何。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，介绍下该监控套件有三个脚本，一个是自动发现主机上的docker容器脚本，另一个是用python写的获取每个docker容器的系统状态，包括CPU使用率，内存使用率以及网络资源使用率，最后这个脚本添加了一些我公司常用的应用的状态监控，包括应用占用内存，cpu资源以及进程的存活状态，至于读者们需要监控其他docker里面的应用，可以依照我的脚本来进行修改。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要编辑自动发现docker中容器个数的脚本，内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">cat /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh </div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#Fucation:docker low-level discovery</span></div><div class="line"><span class="function"><span class="title">docker</span></span>() &#123;</div><div class="line"> port=($(sudo docker ps -a|grep -v <span class="string">"CONTAINER ID"</span>|awk <span class="string">'&#123;print $NF&#125;'</span>))</div><div class="line"> <span class="built_in">printf</span> <span class="string">'&#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t"data":[\n'</span></div><div class="line"> <span class="keyword">for</span> key <span class="keyword">in</span> <span class="variable">$&#123;!port[@]&#125;</span></div><div class="line"> <span class="keyword">do</span></div><div class="line"> <span class="keyword">if</span> [[ <span class="string">"<span class="variable">$&#123;#port[@]&#125;</span>"</span> -gt 1 &amp;&amp; <span class="string">"<span class="variable">$&#123;key&#125;</span>"</span> -ne <span class="string">"<span class="variable">$(($&#123;#port[@]&#125;-1)</span>)"</span> ]];<span class="keyword">then</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t &#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">"\t\t\t\"&#123;#CONTAINERNAME&#125;\":\"<span class="variable">$&#123;port[$&#123;key&#125;</span>]&#125;\"&#125;,\n"</span></div><div class="line">  </div><div class="line"> <span class="keyword">else</span> [[ <span class="string">"<span class="variable">$&#123;key&#125;</span>"</span> -eq <span class="string">"((<span class="variable">$&#123;#port[@]&#125;</span>-1))"</span> ]]</div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t &#123;\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">"\t\t\t\"&#123;#CONTAINERNAME&#125;\":\"<span class="variable">$&#123;port[$&#123;key&#125;</span>]&#125;\"&#125;\n"</span></div><div class="line">  </div><div class="line"> <span class="keyword">fi</span></div><div class="line"> <span class="keyword">done</span></div><div class="line">  </div><div class="line"> <span class="built_in">printf</span> <span class="string">'\t ]\n'</span></div><div class="line"> <span class="built_in">printf</span> <span class="string">'&#125;\n'</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">docker)</div><div class="line">docker</div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage:`basename <span class="variable">$0</span>` &#123;docker&#125;"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这个脚本是用来获取到docker里面应用的容器，并对其进行json化输出的，效果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">sh /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh docker</div><div class="line">&#123;</div><div class="line"><span class="string">"data"</span>:[</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"hopeful_brown"</span>&#125;,</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"happy_einstein"</span>&#125;</div><div class="line"> ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就能被zabbix_server获取到了，然后是python脚本,使用python获取docker的参数需要使用一个扩展包，可以通过pip或者easy_install安装docker-py扩展：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pip install docker-py或者easy_install docker-py或者不想这样安装的话可以去    python官网下载docker-py的安装包，解压后使用docker docker-py-1.4.0/setup.py     install命令安装，扩展包我将会打包放在附件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是python脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line">cat /usr/<span class="built_in">local</span>/zabbix/scripts/docker_monitor.py </div><div class="line"><span class="comment">#!/usr/bin/env python</span></div><div class="line"><span class="comment">#-*- coding: utf-8 -*-</span></div><div class="line"><span class="comment">#author:Xianglin Hu</span></div><div class="line"><span class="comment">#email: a714585725@qq.com</span></div><div class="line">from docker import Client</div><div class="line">import sys</div><div class="line">import subprocess</div><div class="line">import os</div><div class="line"> </div><div class="line">def check_container_stats(container_name,collect_item):</div><div class="line"> container_collect=docker_client.stats(container_name)</div><div class="line"> old_result=<span class="built_in">eval</span>(container_collect.next())</div><div class="line"> new_result=<span class="built_in">eval</span>(container_collect.next())</div><div class="line"> container_collect.close()</div><div class="line"> <span class="keyword">if</span> collect_item == <span class="string">'cpu_total_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'cpu_system_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'cpu_percent'</span>:</div><div class="line"> cpu_total_usage=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'total_usage'</span>]</div><div class="line"> cpu_system_uasge=new_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>] - old_result[<span class="string">'cpu_stats'</span>][<span class="string">'system_cpu_usage'</span>]</div><div class="line"> cpu_num=len(old_result[<span class="string">'cpu_stats'</span>][<span class="string">'cpu_usage'</span>][<span class="string">'percpu_usage'</span>])</div><div class="line"> result=round((<span class="built_in">float</span>(cpu_total_usage)/<span class="built_in">float</span>(cpu_system_uasge))*cpu_num*100.0,2)</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_usage'</span>:</div><div class="line"> result=new_result[<span class="string">'memory_stats'</span>][<span class="string">'usage'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_limit'</span>:</div><div class="line"> result=new_result[<span class="string">'memory_stats'</span>][<span class="string">'limit'</span>]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'mem_percent'</span>:</div><div class="line"> mem_usage=new_result[<span class="string">'memory_stats'</span>][<span class="string">'usage'</span>]</div><div class="line"> mem_limit=new_result[<span class="string">'memory_stats'</span>][<span class="string">'limit'</span>]</div><div class="line"> result=round(<span class="built_in">float</span>(mem_usage)/<span class="built_in">float</span>(mem_limit)*100.0,2)</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'network_rx_bytes'</span>:</div><div class="line"> network_check_command=<span class="string">""</span><span class="string">"sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '&#123;print <span class="variable">$2</span>,<span class="variable">$10</span>&#125;'"</span><span class="string">""</span>%container_name</div><div class="line"> result=os.popen(network_check_command).<span class="built_in">read</span>().split()[0]</div><div class="line"> <span class="keyword">elif</span> collect_item == <span class="string">'network_tx_bytes'</span>:</div><div class="line"> network_check_command=<span class="string">""</span><span class="string">"sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk '&#123;print <span class="variable">$2</span>,<span class="variable">$10</span>&#125;'"</span><span class="string">""</span>%container_name</div><div class="line"> result=os.popen(network_check_command).<span class="built_in">read</span>().split()[1]</div><div class="line"> <span class="built_in">return</span> result</div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</div><div class="line"> docker_client = Client(base_url=<span class="string">'unix://var/run/docker.sock'</span>, version=<span class="string">'1.17'</span>)</div><div class="line"> container_name=sys.argv[1]</div><div class="line"> collect_item=sys.argv[2]</div><div class="line"> <span class="built_in">print</span> check_container_stats(container_name,collect_item)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里面使用到了docker里面的Client类，获取到某个docker容器的当前状态信息，然后进行运算，返回运算结果。但是容器当前信息那个dict中的network的key获取到的信息不准确，于是我使用了docker exec命令来获取docker容器内部的网络流量信息。这也是在大神的基础上进行改进的地方，这里的改进将python脚本的执行时间缩短了，将有助于server获取duocker容器信息时减少长连接的数量，提升zabbix_server的性能。这里的docker exec命令将会在下一个脚本中大量使用来获取docker容器中的应用状态信息。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是获取容器应用状态信息的脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">cat docker_processmonitor.sh </div><div class="line"></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#license:GPL</span></div><div class="line"><span class="comment">#mail:a714585725@qq.com</span></div><div class="line"><span class="comment">#date:2015.09.22</span></div><div class="line"><span class="function"><span class="title">processmem</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ps aux|grep <span class="variable">$2</span>|grep -v <span class="string">"grep"</span>|grep -v <span class="string">"processstatus.sh"</span>|awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">processcpu</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ps aux|grep <span class="variable">$2</span>|grep -v <span class="string">"grep"</span>|grep -v <span class="string">"processstatus.sh"</span>|awk <span class="string">'&#123;sum+=$3&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">processport</span></span>()&#123;</div><div class="line"> sudo /usr/bin/docker <span class="built_in">exec</span> <span class="variable">$1</span> ss -antlp|grep <span class="variable">$2</span>|grep LISTEN|wc -l</div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$3</span>"</span> <span class="keyword">in</span></div><div class="line">mem)</div><div class="line">processmem <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">cpu)</div><div class="line">processcpu <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">port)</div><div class="line">processport <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;docker_containername&#125;&#123;processname&#125;&#123;mem|cpu|port&#125;"</span></div><div class="line">;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个脚本使用了一个case来判断需要获取的docker容器的名称以及该容器中应用的状态信息，只不过这里获取docker容器状态信息使用的是docker exec命令来进行获取。另外这里面添加了对于应用是否存活的状态监测，那就是检测该应用是否侦听了网络端口，假如该应用侦听的网络端口个数为0的话，可以认为该应用存在异常。可以根据自己需求修改相应应用的监控。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于docker是以root权限来启用的，而zabbix监控是使用zabbix用户来执行的，所以需要给予zabbix用户相应的权限，需要编辑visudo：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> <span class="string">"zabbix ALL=(root) 	NOPASSWD:/bin/docker,/usr/bin/python,/usr/local/zabbix/scripts/docker_monitor.py,/usr/local/zabbix/scripts/docker_low_discovery.sh,/usr/local/zabbix/scripts/docker_processmonitor.sh"</span>&gt;&gt;/etc/sudoers</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并且还需要注释掉这条记录：#Defaults    requiretty（PS：注意，这条记录是要求使用sudo命令时需要有终端界面，注释掉这一条之后就可以不需要终端执行sudo命令了。）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后就是编辑zabbix_agentd.conf文件,添加下面几行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">tail -3 /usr/<span class="built_in">local</span>/zabbix/etc/zabbix_agentd.conf</div><div class="line"></div><div class="line">UserParameter=docker_low_discovery[*],/bin/bash /usr/<span class="built_in">local</span>/zabbix/scripts/docker_low_discovery.sh <span class="variable">$1</span></div><div class="line">UserParameter=docker_stats[*],sudo /usr/bin/python /usr/<span class="built_in">local</span>/zabbix/scripts/docker_monitor.py <span class="variable">$1</span> <span class="variable">$2</span></div><div class="line">UserParameter=docker_process[*],/bin/bash /usr/<span class="built_in">local</span>/zabbix/scripts/docker_processmonitor.sh <span class="variable">$1</span> <span class="variable">$2</span> <span class="variable">$3</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存配置并重启zabbix_agentd服务，然后修改脚本的属主属组以及权限：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">chown zabbix.zabbix /usr/<span class="built_in">local</span>/zabbix/scripts/*</div><div class="line">chmod 755 /usr/<span class="built_in">local</span>/zabbix/scripts/*</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后可以在zabbix_server端测试是否能够获取到相应的数据：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_low_discovery[docker]"</span></div><div class="line">&#123;</div><div class="line"><span class="string">"data"</span>:[</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"hopeful_brown"</span>&#125;,</div><div class="line"> &#123;</div><div class="line"><span class="string">"&#123;#CONTAINERNAME&#125;"</span>:<span class="string">"happy_einstein"</span>&#125;</div><div class="line"> ]</div><div class="line">&#125;</div><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_stats[happy_einstein,network_tx_bytes]"</span></div><div class="line">9664252</div><div class="line">[root@test1 ~]<span class="comment"># /usr/local/zabbix-2.4.4/bin/zabbix_get -s x.x.x.x -k"docker_process[happy_einstein,nginx,port]"</span></div><div class="line">2</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的IP地址就用x.x.x.x代替了，这里应该填写客户端的IP地址。如上所示，能够正确获取到agentd的数据以后，然后就需要在zabbix_server这边配置监控模版了</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/01.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/02.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/03.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/04.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%87%AA%E5%8A%A8%E5%8F%91%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E7%9B%91%E6%8E%A7docker%E7%8A%B6%E6%80%81/05.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这些显示的是监控的结果，也可以根据自己的需求来进行修改</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/25. zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数/">zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，在zabbix脚本目录下添加一个脚本，写入如下代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#license:GPL</span></div><div class="line"><span class="comment">#mail:admin@huxianglin.cn</span></div><div class="line"><span class="comment">#date:2015.04.16</span></div><div class="line">top -bn1&gt;/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/process.log</div><div class="line">LOG=/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/process.log</div><div class="line"><span class="function"><span class="title">php_fpm</span></span>()&#123;</div><div class="line">	grep <span class="string">"php-fpm"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">php_fpm_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"php-fpm"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">nginx</span></span>()&#123;</div><div class="line">        grep <span class="string">"nginx"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">nginx_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"nginx"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">mysqld</span></span>()&#123;</div><div class="line">        grep <span class="string">"mysqld"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">mysqld_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"mysqld"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">php_fpm)</div><div class="line">	php_fpm</div><div class="line">	;;</div><div class="line">php_fpm_num)</div><div class="line">	php_fpm_num</div><div class="line">	;;</div><div class="line">nginx)</div><div class="line">	nginx</div><div class="line">	;;</div><div class="line">nginx_num)</div><div class="line">	nginx_num</div><div class="line">	;;</div><div class="line">mysqld)</div><div class="line">	mysqld</div><div class="line">	;;</div><div class="line">mysqld_num)</div><div class="line">	mysqld_num</div><div class="line">	;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;php_fpm|php_fpm_num|nginx|nginx_num|mysqld|mysqld_num&#125;"</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存之后修改其属组和属主为zabbix.zabbix,然后新建一个文件process.log，同样设置属主和属组为zabbix.zabbix</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑zabbix_agentd.conf文件，在最后添加上下面这段代码，并重启zabbix_agentd服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">UserParameter=process.php_fpm,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh php_fpm</div><div class="line">UserParameter=process.php_fpm_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh php_fpm_num</div><div class="line">UserParameter=process.nginx,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh nginx</div><div class="line">UserParameter=process.nginx_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh nginx_num</div><div class="line">UserParameter=process.mysqld,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh mysqld</div><div class="line">UserParameter=process.mysqld_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh mysqld_num</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后就可以在web页面添加item，生成图表了，注意top取到的内存单位是KB，所以在定义item的时候需要自定义单位和设定倍数</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生成的各个程序占用内存大小的图形如下</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/02.jpeg?raw=true" alt=""></figure></p>
<p>生成的各个程序的进程个数图形如下，可以设定触发器，当进程满足触发其条件时发送报警</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/03.jpeg?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/23. zabbix使用pyora监控oracle数据库/">zabbix使用pyora监控oracle数据库</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用Orabbix监控oracle数据库，发现可以实现对oracel数据库实行监控，但是    最近部署Orabbix监控oracle数据库，发现始终无法获取到数据库的内容。。。。。。由于Orabbix是基于java实现的，使用的是jdbc连接oracle数据库，获取到的值传递给zabbix捕捉器来捕捉，这个过程我不是很熟悉。。。。。。始终无法调试出图来。。。最后终于放弃，寻找Orabbix的替代品。在zabbix官网找了一遍，发现使用python实现的pyora这个脚本还不错，最大的优势就是能够自定义自己想监控的相关参数，只需要修改脚本，在里面添加相关函数就行。pyora是通过python使用python的一个组件cx_Oracle来获取的，获取到的数据传递给zabbix的agent，从而获取到相关监控数据，这种方式比较简单，也比较容易扩展。pyora的缺点就是本身提供的监控item比较少，而且提供的模板是一个半成品模板，需要自己根据需要设计相应的模板，从而达到监控的目的。总体来说，pyora的特点就是简单，开源，易扩展，但是整体做的不是很完整。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要下载pyora，在zabbix官网上面找到的github的链接，链接在这<a href="https://github.com/bicofino/Pyora" target="_blank" rel="external">https://github.com/bicofino/Pyora</a> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然而python需要安装cx_Oracle这个组件才能连接到数据库，安装cx_Oracle这个组件比较麻烦，搞了差不多两个多小时才搞定，开始使用源码安装，结果安装不上去。。。一直解决不了报错。。。于是去找了rpm包来安装，由于这里的oracle数据库的python版本是2.6的，所以下载了2.6对应的版本，并且这里支持的最新的oracle也只有11g，而我这里使用的是12c，当时还担心了好久，怕无法使用，后来证实11g的rpm包也是可以在12c的oracle数据库上面使用的，链接在这<a href="http://sourceforge.net/projects/cx-oracle/files/5.1.2/cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm/download" target="_blank" rel="external">http://sourceforge.net/projects/cx-oracle/files/5.1.2/cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm/download</a> 可以根据自己的需要下载相应的rpm包。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还需要下载两个python的组件，一个是argparse，还有一个是setuptools这两个包都是到Python官网下载的。首先安装setuptools，然后再安装argparse，安装方式是使用源码安装，解压后进入目录，使用python setup.py install就可以安装了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，前期准备工作做完了，接下来就需要安装cx_Oracle了，首先，在root用户下    的.bashrc中添加oracle的环境变量，否则将导致无法使用cx_Oracle模块，下面是在用    户中添加的环境变量：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">tail -6 .bashrc </div><div class="line"><span class="comment"># User specific aliases and functions</span></div><div class="line"><span class="built_in">export</span> ORACLE_HOME=/u01/app/oracle/product/12.1.0.2/dbhome</div><div class="line"><span class="built_in">export</span> TNS_ADMIN=<span class="variable">$ORACLE_HOME</span>/network/admin</div><div class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$ORACLE_HOME</span>/bin:<span class="variable">$ORACLE_HOME</span>/lib:/lib:/usr/lib:<span class="variable">$ORACLE_HOME</span>/rdbms/lib</div><div class="line"><span class="built_in">export</span> LD_LIBRARY_PATH=<span class="variable">$ORACLE_HOME</span>/lib:/lib:/usr/lib:<span class="variable">$ORACLE_HOME</span>/rdbms/lib</div><div class="line"><span class="built_in">export</span> CLASSPATH=<span class="variable">$ORACLE_HOME</span>/JRE:<span class="variable">$ORACLE_HOME</span>/jlib:<span class="variable">$ORACLE_HOME</span>/rdbms/jlib</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加好环境变量之后，需要使其立即生效，使用命令source .bashrc来使其生效，注意<code>export     PATH=$PATH:$ORACLE_HOME/bin:$ORACLE_HOME/lib:/lib:/usr/lib:$ORACLE_HOME/rdbms/lib</code>这条命令，刚开始的时候没有添加后面的lib的环境变量，导致安装好cx_Oracle    组件之后还是无法import这个组件，报错信息如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">error <span class="keyword">while</span> loading shared libraries: libclntsh.so.11.1: cannot open shared object file: No such file or directory</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;后来，将环境变量中添加了lib库该报错就消失了。接下来就是安装cx_Oracle组建了，使用rpm -ivh cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm就可以安装了，安装好后使用下面的操作看是否能够正常的import，下面是我的操作步骤，如果你的和我的一样没有异常输出，那么就没问题的：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">python</div><div class="line">Python 2.6.6 (r266:84292, Jan 22 2014, 01:49:05) </div><div class="line">[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2</div><div class="line">Type <span class="string">"help"</span>, <span class="string">"copyright"</span>, <span class="string">"credits"</span> or <span class="string">"license"</span> <span class="keyword">for</span> more information.</div><div class="line">&gt;&gt;&gt; import cx_Oracle</div><div class="line">&gt;&gt;&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装好后就可以使用pyora脚本来获取值了，（PS:对了，在这之前，需要在oracle数据库中创建相应的监控用户）关于oracle数据库中添加相应的监控用户权限属于DBA的范畴了，我这里只会贴出一些参考的权限配置，相关配置各位可以和公司的DBA协商配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">CREATE USER ZABBIX IDENTIFIED BY 123456 DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP;</div><div class="line"> GRANT CONNECT TO ZABBIX;</div><div class="line"> GRANT RESOURCE TO ZABBIX;</div><div class="line"> ALTER USER ZABBIX DEFAULT ROLE ALL;</div><div class="line"> GRANT SELECT ANY TABLE TO ZABBIX;</div><div class="line"> GRANT CREATE SESSION TO ZABBIX;</div><div class="line"> GRANT SELECT ANY DICTIONARY TO ZABBIX;</div><div class="line"> GRANT UNLIMITED TABLESPACE TO ZABBIX;</div><div class="line"> GRANT SELECT ANY DICTIONARY TO ZABBIX;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置好环境后就可以使用pyora脚本实现监控oracle了，接下来需要执行pyora脚本来进行测试了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在下载的pyora压缩包中含有pyora.py脚本，将其上传至客户端服务器，然后使用python pyora.py –username zabbix –password 123456 –address 127.0.0.1 –database xxx show_users来测试是否能够获取到正常数据，下面是执行的结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="string">"data"</span>: [&#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ANONYMOUS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"APPQOSSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ASYNC"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"AUDSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"DBSNMP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"DIP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMADMIN_INTERNAL"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMCATUSER"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMUSER"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"OJVMSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ORACLE_OCM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"OUTLN"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYNC"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSBACKUP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSDG"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSKM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSTEM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"WMSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"XDB"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"XS<span class="variable">$NULL</span>"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ZABBIX"</span>&#125;]&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;获取的是json格式的数据，证明这个脚本是可以正常运行的，然后需要创建zabbix用户，然后再安装agnetd，然后修改zabbix用户的环境变量，编辑zabbix用户家目录下的.bashrc将上面的环境变量添加进去并source .bashrc使环境变量生效。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后再使用zabbix用户重复一次执行上面的获取数据的步骤，测试能够获取数据的话就说明没问题了，在agentd的配置文件中写入如下配置，以执行这个脚本获取到监控的数据：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tail -2 /home/zabbix/zabbix/etc/zabbix_agentd.conf</div><div class="line">UserParameter=pyora[*],/usr/bin/python /home/zabbix/zabbix/scripts/pyora.py --username <span class="variable">$1</span> --password <span class="variable">$2</span> --address <span class="variable">$3</span> --database <span class="variable">$4</span> <span class="variable">$5</span> <span class="variable">$6</span> <span class="variable">$7</span> <span class="variable">$8</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后重启agentd，然后再在server端使用zabbix_get测试能否获取到数据，测试结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/opt/software/zabbix/bin/zabbix_get -s xx.xx.xx.xx -k<span class="string">"pyora[zabbix,123456,127.0.0.1,xxx,show_users]"</span></div><div class="line">&#123;<span class="string">"data"</span>: [&#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ANONYMOUS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"APPQOSSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ASYNC"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"AUDSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"DBSNMP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"DIP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMADMIN_INTERNAL"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMCATUSER"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"GSMUSER"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"OJVMSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ORACLE_OCM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"OUTLN"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYNC"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSBACKUP"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSDG"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSKM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"SYSTEM"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"WMSYS"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"XDB"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"XS<span class="variable">$NULL</span>"</span>&#125;, &#123;<span class="string">"&#123;#DBUSER&#125;"</span>: <span class="string">"ZABBIX"</span>&#125;]&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;证明没有问题了，然后需要在zabbix_server的web端添加模板，这个模板在下载的pyora压缩包里面有一个半成品的模板，这个模板是个不完整的半成品，需要自己完成剩下的配置我这里稍后也会打包放上我这边进行部分修改后的模板上来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加主机并添加模板，这时候需要注意，因为这个模板中使用了宏变量，所以在添加主机的时候需要设定相应的宏变量，下面是设定宏变量的截图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8pyora%E7%9B%91%E6%8E%A7oracle%E6%95%B0%E6%8D%AE%E5%BA%93/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设定好宏之后，等待一段时间（ps:一般5到十分钟就能够正常的产生数据了），下面是获取到的监控数据的截图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8pyora%E7%9B%91%E6%8E%A7oracle%E6%95%B0%E6%8D%AE%E5%BA%93/02.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8pyora%E7%9B%91%E6%8E%A7oracle%E6%95%B0%E6%8D%AE%E5%BA%93/03.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8pyora%E7%9B%91%E6%8E%A7oracle%E6%95%B0%E6%8D%AE%E5%BA%93/04.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8pyora%E7%9B%91%E6%8E%A7oracle%E6%95%B0%E6%8D%AE%E5%BA%93/05.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，至此，简单的oracle监控已经结束了，至于更多的监控需求，各位可以依据自己的需求进行适当的修改脚本，以达到各位的要求。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/24. zabbix通过JMX监控tomcat状态/">zabbix通过JMX监控tomcat状态</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为大量使用tomcat作为应用服务，所以，这两天催生了一个想法，通过zabbix监控tomcat的运行状态，从而能够更快的发现tomcat服务出现的问题以及判断问题出现在哪块。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，zabbix监控tomcat等这一类java平台的应用不是直接通过agentd来实现的，而是使用jmx来获取到tomcat这类应用的状态，然后再将数据交给server端，生成监控图。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果需要用到jmx监控的话需要在客户端和服务端都安装java环境，至于java环境可以通过源码安装，不过我这里为了省事，直接通过yum安装java和java-devel这两个包：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install java java-devel</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后是服务端的配置：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务端需要重新安装zabbix服务端，需要将–enable-java添加到编译参数中去，参数如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/zabbix/ --<span class="built_in">enable</span>-server --<span class="built_in">enable</span>-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --<span class="built_in">enable</span>-java</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装好服务端之后可以在软件安装目录找到如下这个目录：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@test1 zabbix_java]<span class="comment"># pwd</span></div><div class="line">/usr/<span class="built_in">local</span>/zabbix/sbin/zabbix_java</div><div class="line">[root@test1 zabbix_java]<span class="comment"># ls</span></div><div class="line">bin lib settings.sh shutdown.sh startup.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改settings.sh中得参数，其中大部分都是以#开头的，修改的参数如下面所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@test1 zabbix_java]<span class="comment"># egrep -v "^#|^$" settings.sh </span></div><div class="line">LISTEN_IP=<span class="string">"0.0.0.0"</span></div><div class="line">LISTEN_PORT=10052</div><div class="line">PID_FILE=<span class="string">"/tmp/zabbix_java.pid"</span></div><div class="line">START_POLLERS=5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改zabbix_server.conf的配置，修改的参数如下面所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@test1 zabbix_java]<span class="comment"># egrep -v "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf</span></div><div class="line">LogFile=/tmp/zabbix_server.log</div><div class="line">DBName=zabbix</div><div class="line">DBUser=zabbix</div><div class="line">DBPassword=xxxxxx</div><div class="line">DBSocket=/tmp/mysql.sock</div><div class="line">JavaGateway=127.0.0.1</div><div class="line">JavaGatewayPort=10052</div><div class="line">StartJavaPollers=5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中上面的5行和以前配置一样，下面的三行是需要将前面的#删掉，并进行修改的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改完以后，重启zabbixserver并且启动zabbix_java_Gateway启动    zabbix_java_Gateway的方法为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sh /usr/<span class="built_in">local</span>/zabbix/sbin/zabbix_java/startup.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动之后，在服务端会多出一个侦听端口10052来，效果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@test1 zabbix_java]<span class="comment"># netstat -antlp|grep 10052</span></div><div class="line">tcp6 0 0 :::10052 :::* LISTEN 15371/java </div><div class="line">tcp6 0 0 127.0.0.1:10052 127.0.0.1:38661 TIME_WAIT</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看到上面这些信息，证明服务端已经配置好了，接下来需要配置tomcat客户端了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tomcat客户端的配置为如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先安装java环境</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也和上面一样使用yum安装：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install java java-devel</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后下载tomcat的源码包，解压后放在相应的目录，我这里放在/usrlocal/tomcat-    8.0.26/目录下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后需要下载对应tomcat的jmx版本，这里安装的是最新的tomcat,版本是8.0.26，于是下载路径为如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://archive.apache.org/dist/tomcat/tomcat-	8/v8.0.26/bin/extras/catalina-jmx-remote.jar</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用其他版本的可以自行选择相对应的jmx版本下载。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载后将该文件放在tomcat的lib目录下，然后修改catalina.sh文件，catalina.sh文件在tomcat的bin目录下。在#!bin/sh下方添加一行参数，如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line">CATALINA_OPTS=<span class="string">"-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.6.0.176"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中的10.6.0.176是该tomcat客户端的IP。然后需要修改服务器的配置文件server.xml：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@test3 tomcat-8.0.26]<span class="comment"># tail -6 ./conf/server.xml</span></div><div class="line">&lt;Listener className=<span class="string">"org.apache.catalina.mbeans.JmxRemoteLifecycleListener"</span> rmiRegistryPortPlatform=<span class="string">"8090"</span> rmiServerPortPlatform=<span class="string">"8090"</span> /&gt;</div><div class="line"> &lt;/Host&gt;</div><div class="line"> &lt;/Engine&gt;</div><div class="line"> &lt;/Service&gt;</div><div class="line">&lt;/Server&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如上面所示，第一行就是我们添加进去的配置，其中的8090就是zabbix server端监控tomcat需要使用的端口。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存之后退出，然后启动tomcat和zabbix的agentd。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动完成之后通过ps命令和netstat命令查看是否正常的侦听了相应的端口，zabbix_agentd默认侦听10050端口，tomcat默认侦听8080端口，8090端口是开给jmx使用    的，应该也是开启的，效果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@test3 tomcat-8.0.26]<span class="comment"># netstat -antlp|grep LISTEN </span></div><div class="line">tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 19592/./sbin/zabbix </div><div class="line">tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 836/sshd </div><div class="line">tcp6 0 0 ::1:25 :::* LISTEN 1503/master </div><div class="line">tcp6 0 0 :::8090 :::* LISTEN 21026/java </div><div class="line">tcp6 0 0 127.0.0.1:8005 :::* LISTEN 21026/java </div><div class="line">tcp6 0 0 :::8009 :::* LISTEN 21026/java </div><div class="line">tcp6 0 0 :::8080 :::* LISTEN 21026/java </div><div class="line">tcp6 0 0 :::40438 :::* LISTEN 21026/java </div><div class="line">tcp6 0 0 :::22 :::* LISTEN 836/sshd</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时候可以在zabbix_server端尝试获取tomcat的相关数据了。这时候需要使用到一个工具cmdline-jmxclient-0.10.3.jar</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个工具可以自己到网上下载，我的附件中也会有这个工具，使用方法为如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@test1 zabbix_java]<span class="comment"># java -jar /root/cmdline-jmxclient-0.10.3.jar - 10.6.0.176:8090 java.lang:type=Memory NonHeapMemoryUsage</span></div><div class="line">09/09/2015 17:56:49 +0800 org.archive.jmx.Client NonHeapMemoryUsage: </div><div class="line">committed: 24313856</div><div class="line">init: 24313856</div><div class="line">max: 224395264</div><div class="line">used: 20066808</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面已经获取到了相应的tomcat数据，好了，到这里配置就差不多要大功告成了，但是还需要在web端配置相应的模版，刚开始我使用系统自带的模板，发现出不来数据，于是我去网上找到了某个网友自己做的模板，终于可以获取到tomcat的数据了，模板我也将一并打包放在附件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，在web端开始配置了，配置如下图所示：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87JMX%E7%9B%91%E6%8E%A7tomcat%E7%8A%B6%E6%80%81/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中的端口需要填写两个，第一个是agentd的侦听端口，第二个是jmx的侦听端口，这个端口8090是我们在tomcat客户端的server.xml文件中定义的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将该主机关联到相应的模板上去：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87JMX%E7%9B%91%E6%8E%A7tomcat%E7%8A%B6%E6%80%81/02.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后就等待出数据了，该模板中的某些键值在tomcat中不支持，我目前还不知道是什么情况，可能和tomcat中得配置参数有关系吧。下面展示一下出图的效果，这个模板各位也可以拿回去自己进行修改：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87JMX%E7%9B%91%E6%8E%A7tomcat%E7%8A%B6%E6%80%81/03.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87JMX%E7%9B%91%E6%8E%A7tomcat%E7%8A%B6%E6%80%81/04.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87JMX%E7%9B%91%E6%8E%A7tomcat%E7%8A%B6%E6%80%81/05.jpeg?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/22. zabbix使用percona zabbix mysql-plugin监控mysql数据库/">zabbix使用percona zabbix mysql-plugin监控mysql数据库</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于zabbix自带的mysql监控模板监控的东西比较少，使用percona zabbix mysql-plugin实现对mysql的监控。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;percona zabbix mysql-plugin是percona发布的一个使用zabbix监控mysql数据库的工具，这款工具比zabbix自带的监控模板要强大的多，毕竟percona是Mysql的一个重要分支，专业做数据库的，所以，采集的数据比较全面。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面开始进入正题，部署mysql的监控。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要安装安装php和php-mysql，因为用到了php脚本，所以在本机上面需要安装php，至于php的安装不是本文讨论的重点</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，需要去percona官网下载最新版本的percona zabbix mysql-plugin，从1.1版本开始起支持zabbix，包括cacti，nagios也都有接口提供，这里下载的是<a href="https://www.percona.com/downloads/percona-monitoring-plugins/1.1.5/percona-zabbix-templates-1.1.5-1.noarch.rpm" target="_blank" rel="external">percona zabbix mysql-plugin</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载后安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rpm -ivh percona-zabbix-templates-1.1.5-1.noarch.rpm</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完毕后会产生两个目录，每个目录有两个文件，详细结果如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@test2 ~]<span class="comment"># ls /var/lib/zabbix/percona/</span></div><div class="line">scripts templates</div><div class="line">[root@test2 ~]<span class="comment"># ls /var/lib/zabbix/percona/scripts/</span></div><div class="line">get_mysql_stats_wrapper.sh ss_get_mysql_stats.php</div><div class="line">[root@test2 ~]<span class="comment"># ls /var/lib/zabbix/percona/templates/</span></div><div class="line">userparameter_percona_mysql.conf zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.5.xml</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，需要下载模版文件zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.5.xml然后将这个模    板文件导入到zabbixweb端的模板中去，然后将配置文    userparameter_percona_mysql.conf放入到zabbix的配置文件目录中，我这的目录    是/usr/local/zabbix-2.4.4/etc/zabbix_agentd.conf.d/</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改zabbix_agentd.conf配置文件，将配置文件所在目录添加进去，也就是添加下面这行语句：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Include=/usr/<span class="built_in">local</span>/zabbix-2.4.4/etc/zabbix_agentd.conf.d/*.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改脚本，首先修改sh脚本，也就是get_mysql_stats_wrapper.sh这个脚本，将下面这条语句进行修改：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">RES=`HOME=~zabbix mysql -e <span class="string">'SHOW SLAVE STATUS\G'</span> | egrep <span class="string">'(Slave_IO_Running|Slave_SQL_Running):'</span> | awk -F: <span class="string">'&#123;print $2&#125;'</span> | tr <span class="string">'\n'</span> <span class="string">','</span>`</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">RES=`/usr/<span class="built_in">local</span>/mysql/bin/mysql -uroot -e <span class="string">'SHOW SLAVE STATUS\G'</span> | egrep <span class="string">'(Slave_IO_Running|Slave_SQL_Running):'</span> | awk -F: <span class="string">'&#123;print $2&#125;'</span> | tr <span class="string">'\n'</span> <span class="string">','</span>`</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里Mysql没有设置root密码，可以在这里写入mysql的相应账号密码。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有需要注意，在这个脚本中需要调用php来实现监控，所以需要修改php的路径，我的php的路径为/usr/local/php/bin/php，所以将脚本中的那条命令修改成下面这样：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CMD=<span class="string">"/usr/local/php/bin/php -q <span class="variable">$DIR</span>/ss_get_mysql_stats.php --host <span class="variable">$HOST</span> --items gg"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后保存该脚本，并修改权限为755<br>然后修改php脚本文件，修改用户名和密码如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="variable">$mysql_user</span> = <span class="string">'root'</span>;</div><div class="line"><span class="variable">$mysql_pass</span> = <span class="string">''</span>;</div><div class="line"><span class="variable">$mysql_port</span> = 3306;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为mysqlroot密码是空的，所以这里密码是没有输入的，可以修改成相应的账号密码。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改完之后保存配置并将权限设置为755，然后重启zabbix_agentd，在zabbix_server端添加刚刚我们导入进去的模板，就可以实现mysql的监控了，下面是监控的效果图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8percona%20zabbix%20mysql-plugin%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE%E5%BA%93/01.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8percona%20zabbix%20mysql-plugin%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE%E5%BA%93/02.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8percona%20zabbix%20mysql-plugin%E7%9B%91%E6%8E%A7mysql%E6%95%B0%E6%8D%AE%E5%BA%93/03.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;但是貌似这个模板对于单个数据库虽然足够详细，对于数据库集群的监控还是不够给力，需要在其监控脚本的基础上进行修改，使其能够监控到数据库集群的状态信息那就更好了，当然，我这里只做了单个数据库的监控，集群由于没有实际部署，这里也就没有进行演示了，对这方面感兴趣的童鞋们可以自行阅读脚本源代码，对脚本进行修改以便于实现mysql集群的详细监控。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/21. zabbix使用SNMP V3监控路由器接口流量/">zabbix使用SNMP V3监控路由器接口流量</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于有需要监控网络流量， 于是，部署了zabbix监控路由器流量的应用，下面是通过查找资料，自行实验并成功监控路由器接口流量的过程。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，在路由器上面配置好SNMP协议，我这边配置的是SNMP V3版本协议，并且使用了加密认证的方式(用户名：test认证算法使用SHA-1，密码：abcdefg加密密码使用AES协议，密码：hijklmnopq)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置完之后需要进行检测看能否通过SNMP协议从路由器上面获取数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过在zabbi服务器上面使用snmpwalk，snmpget等工具来尝试获取路由器信息，要使用这两个工具需要先安装net-snmp net-snmp-utils这两个包，才能使用snmpwalk,snmpget等工具测试snmp访问。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完成之后开始调用snmpwalk命令来获取snmp协议的数据。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SNMP V3认证使用方法</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">snmpwalk -v 3 -u <span class="built_in">test</span> -a sha -A abcdefg -x AES -X hijklmnopq -l authPriv 192.168.1.1 interfaces.ifTable.ifEntry.ifOutOctets（或者使用OID号）</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中-v选择版本号</p>
<ul>
<li>-u用户名</li>
<li>-a认证算法为SHA-1</li>
<li>-A认证密码为abcdefg</li>
<li>-x加密协议为AES</li>
<li>-X加密密码为hijklmnopq</li>
<li>-l authPriv启用认证和加密功能</li>
<li>interfaces.ifTable.ifEntry.ifOutOctets（查看接口出去的流量）</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于一些常用的公共的OID号可以百度一下，下面也列出了一些常用的OID号：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;常用SNMPOID号</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div></pre></td><td class="code"><pre><div class="line">Variable Descriptor Object Identifier </div><div class="line">System Group </div><div class="line">sysDescr 1.3.6.1.2.1.1.1 </div><div class="line">sysObjectID 1.3.6.1.2.1.1.2 </div><div class="line">sysUpTime 1.3.6.1.2.1.1.3 </div><div class="line">sysContact 1.3.6.1.2.1.1.4 </div><div class="line">sysName 1.3.6.1.2.1.1.5 </div><div class="line">sysLocation 1.3.6.1.2.1.1.6 </div><div class="line">sysServices 1.3.6.1.2.1.1.7 </div><div class="line"></div><div class="line">interfaces Group </div><div class="line">ifNumber 1.3.6.1.2.1.2.1 </div><div class="line">ifTable 1.3.6.1.2.1.2.2 </div><div class="line">ifEntry 1.3.6.1.2.1.2.2.1 </div><div class="line">ifIndex 1.3.6.1.2.1.2.2.1.1 </div><div class="line">ifDescr 1.3.6.1.2.1.2.2.1.2 </div><div class="line">ifType 1.3.6.1.2.1.2.2.1.3 </div><div class="line">ifMtu 1.3.6.1.2.1.2.2.1.4 </div><div class="line">ifSpeed 1.3.6.1.2.1.2.2.1.5 </div><div class="line">ifPhysAddress 1.3.6.1.2.1.2.2.1.6 </div><div class="line">ifAdminStatus 1.3.6.1.2.1.2.2.1.7 </div><div class="line">ifOperStatus 1.3.6.1.2.1.2.2.1.8 </div><div class="line">ifLastChange 1.3.6.1.2.1.2.2.1.9 </div><div class="line">ifInOctets 1.3.6.1.2.1.2.2.1.10 </div><div class="line">ifInUcastPkts 1.3.6.1.2.1.2.2.1.11 </div><div class="line">ifInNUcastPkts 1.3.6.1.2.1.2.2.1.12 </div><div class="line">ifInDiscards 1.3.6.1.2.1.2.2.1.13 </div><div class="line">ifInErrors 1.3.6.1.2.1.2.2.1.14 </div><div class="line">ifInUnknownProtos 1.3.6.1.2.1.2.2.1.15 </div><div class="line">ifOutOctets 1.3.6.1.2.1.2.2.1.16 </div><div class="line">ifOutUcastPkts 1.3.6.1.2.1.2.2.1.17 </div><div class="line">ifOutNUcastPkts 1.3.6.1.2.1.2.2.1.18 </div><div class="line">ifOutDiscards 1.3.6.1.2.1.2.2.1.19 </div><div class="line">ifOutErrors 1.3.6.1.2.1.2.2.1.20 </div><div class="line">ifOutQLen 1.3.6.1.2.1.2.2.1.21 </div><div class="line">ifSpecific 1.3.6.1.2.1.2.2.1.22 </div><div class="line"></div><div class="line">IP Group </div><div class="line">ipForwarding 1.3.6.1.2.1.4.1 </div><div class="line">ipDefaultTTL 1.3.6.1.2.1.4.2 </div><div class="line">ipInReceives 1.3.6.1.2.1.4.3 </div><div class="line">ipInHdrErrors 1.3.6.1.2.1.4.4 </div><div class="line">ipInAddrErrors 1.3.6.1.2.1.4.5 </div><div class="line">ipForwDatagrams 1.3.6.1.2.1.4.6 </div><div class="line">ipInUnknownProtos 1.3.6.1.2.1.4.7 </div><div class="line">ipInDiscards 1.3.6.1.2.1.4.8 </div><div class="line">ipInDelivers 1.3.6.1.2.1.4.9 </div><div class="line">ipOutRequests 1.3.6.1.2.1.4.10 </div><div class="line">ipOutDiscards 1.3.6.1.2.1.4.11 </div><div class="line">ipOutNoRoutes 1.3.6.1.2.1.4.12 </div><div class="line">ipReasmTimeout 1.3.6.1.2.1.4.13 </div><div class="line">ipReasmReqds 1.3.6.1.2.1.4.14 </div><div class="line">ipReasmOKs 1.3.6.1.2.1.4.15 </div><div class="line">ipReasmFails 1.3.6.1.2.1.4.16 </div><div class="line">ipFragsOKs 1.3.6.1.2.1.4.17 </div><div class="line">ipFragsFails 1.3.6.1.2.1.4.18 </div><div class="line">ipFragCreates 1.3.6.1.2.1.4.19 </div><div class="line">ipAddrTable 1.3.6.1.2.1.4.20 </div><div class="line">ipAddrEntry 1.3.6.1.2.1.4.20.1 </div><div class="line">ipAdEntAddr 1.3.6.1.2.1.4.20.1.1 </div><div class="line">ipAdEntIfIndex 1.3.6.1.2.1.4.20.1.2 </div><div class="line">ipAdEntNetMask 1.3.6.1.2.1.4.20.1.3 </div><div class="line">ipAdEntBcastAddr 1.3.6.1.2.1.4.20.1.4 </div><div class="line">ipAdEntReasmMaxSize 1.3.6.1.2.1.4.20.1.5 </div><div class="line"></div><div class="line">ICMP Group </div><div class="line">icmpInMsgs 1.3.6.1.2.1.5.1 </div><div class="line">icmpInErrors 1.3.6.1.2.1.5.2 </div><div class="line">icmpInDestUnreachs 1.3.6.1.2.1.5.3 </div><div class="line">icmpInTimeExcds 1.3.6.1.2.1.5.4 </div><div class="line">icmpInParmProbs 1.3.6.1.2.1.5.5 </div><div class="line">icmpInSrcQuenchs 1.3.6.1.2.1.5.6 </div><div class="line">icmpInRedirects 1.3.6.1.2.1.5.7 </div><div class="line">icmpInEchos 1.3.6.1.2.1.5.8 </div><div class="line">icmpInEchoReps 1.3.6.1.2.1.5.9 </div><div class="line">icmpInTimestamps 1.3.6.1.2.1.5.10 </div><div class="line">icmpInTimestampReps 1.3.6.1.2.1.5.11 </div><div class="line">icmpInAddrMasks 1.3.6.1.2.1.5.12 </div><div class="line">icmpInAddrMaskReps 1.3.6.1.2.1.5.13 </div><div class="line">icmpOutMsgs 1.3.6.1.2.1.5.14 </div><div class="line">icmpOutErrors 1.3.6.1.2.1.5.15 </div><div class="line">icmpOutDestUnreachs 1.3.6.1.2.1.5.16 </div><div class="line">icmpOutTimeExcds 1.3.6.1.2.1.5.17 </div><div class="line">icmpOutParmProbs 1.3.6.1.2.1.5.18 </div><div class="line">icmpOutSrcQuenchs 1.3.6.1.2.1.5.19 </div><div class="line">icmpOutRedirects 1.3.6.1.2.1.5.20 </div><div class="line">icmpOutEchos 1.3.6.1.2.1.5.21 </div><div class="line">icmpOutEchoReps 1.3.6.1.2.1.5.22 </div><div class="line">icmpOutTimestamps 1.3.6.1.2.1.5.23 </div><div class="line">icmpOutTimestampReps 1.3.6.1.2.1.5.24 </div><div class="line">icmpOutAddrMasks 1.3.6.1.2.1.5.25 </div><div class="line">icmpOutAddrMaskReps 1.3.6.1.2.1.5.26 </div><div class="line"></div><div class="line">TCP Group </div><div class="line">tcpRtoAlgorithm 1.3.6.1.2.1.6.1 </div><div class="line">tcpRtoMin 1.3.6.1.2.1.6.2 </div><div class="line">tcpRtoMax 1.3.6.1.2.1.6.3 </div><div class="line">tcpMaxConn 1.3.6.1.2.1.6.4 </div><div class="line">tcpActiveOpens 1.3.6.1.2.1.6.5 </div><div class="line">tcpPassiveOpens 1.3.6.1.2.1.6.6 </div><div class="line">tcpAttemptFails 1.3.6.1.2.1.6.7 </div><div class="line">tcpEstabResets 1.3.6.1.2.1.6.8 </div><div class="line">tcpCurrEstab 1.3.6.1.2.1.6.9 </div><div class="line">tcpInSegs 1.3.6.1.2.1.6.10 </div><div class="line">tcpOutSegs 1.3.6.1.2.1.6.11 </div><div class="line">tcpRetransSegs 1.3.6.1.2.1.6.12 </div><div class="line">tcpConnTable 1.3.6.1.2.1.6.13 </div><div class="line">tcpConnEntry 1.3.6.1.2.1.6.13.1 </div><div class="line">tcpConnState 1.3.6.1.2.1.6.13.1.1 </div><div class="line">tcpConnLocalAddress 1.3.6.1.2.1.6.13.1.2 </div><div class="line">tcpConnLocalPort 1.3.6.1.2.1.6.13.1.3 </div><div class="line">tcpConnRemAddress 1.3.6.1.2.1.6.13.1.4 </div><div class="line">tcpConnRemPort 1.3.6.1.2.1.6.13.1.5 </div><div class="line">tcpInErrs 1.3.6.1.2.1.6.14 </div><div class="line">tcpOutRsts 1.3.6.1.2.1.6.15 </div><div class="line"></div><div class="line">UDP Group </div><div class="line">udpInDatagrams 1.3.6.1.2.1.7.1 </div><div class="line">udpNoPorts 1.3.6.1.2.1.7.2 </div><div class="line">udpInErrors 1.3.6.1.2.1.7.3 </div><div class="line">udpOutDatagrams 1.3.6.1.2.1.7.4 </div><div class="line">udpTable 1.3.6.1.2.1.7.5 </div><div class="line">udpEntry 1.3.6.1.2.1.7.5.1 </div><div class="line">udpLocalAddress 1.3.6.1.2.1.7.5.1.1 </div><div class="line">udpLocalPort 1.3.6.1.2.1.7.5.1.2 </div><div class="line"></div><div class="line">SNMP Group </div><div class="line">snmpInPkts 1.3.6.1.2.1.11.1 </div><div class="line">snmpOutPkts 1.3.6.1.2.1.11.2 </div><div class="line">snmpInBadVersions 1.3.6.1.2.1.11.3 </div><div class="line">snmpInBadCommunityNames 1.3.6.1.2.1.11.4 </div><div class="line">snmpInBadCommunityUses 1.3.6.1.2.1.11.5 </div><div class="line">snmpInASNParseErrs 1.3.6.1.2.1.11.6 </div><div class="line">NOT USED 1.3.6.1.2.1.11.7 </div><div class="line">snmpInTooBigs 1.3.6.1.2.1.11.8 </div><div class="line">snmpInNoSuchNames 1.3.6.1.2.1.11.9 </div><div class="line">snmpInBadValues 1.3.6.1.2.1.11.10 </div><div class="line">snmpInReadOnlys 1.3.6.1.2.1.11.11 </div><div class="line">snmpInGenErrs 1.3.6.1.2.1.11.12 </div><div class="line">snmpInTotalReqVars 1.3.6.1.2.1.11.13 </div><div class="line">snmpInTotalSetVars 1.3.6.1.2.1.11.14 </div><div class="line">snmpInGetRequests 1.3.6.1.2.1.11.15 </div><div class="line">snmpInGetNexts 1.3.6.1.2.1.11.16 </div><div class="line">snmpInSetRequests 1.3.6.1.2.1.11.17 </div><div class="line">snmpInGetResponses 1.3.6.1.2.1.11.18 </div><div class="line">snmpInTraps 1.3.6.1.2.1.11.19 </div><div class="line">snmpOutTooBigs 1.3.6.1.2.1.11.20 </div><div class="line">snmpOutNoSuchNames 1.3.6.1.2.1.11.21 </div><div class="line">snmpOutBadValues 1.3.6.1.2.1.11.22 </div><div class="line">NOT USED 1.3.6.1.2.1.11.23 </div><div class="line">snmpOutGenErrs 1.3.6.1.2.1.11.24 </div><div class="line">snmpOutGetRequests 1.3.6.1.2.1.11.25 </div><div class="line">snmpOutGetNexts 1.3.6.1.2.1.11.26 </div><div class="line">snmpOutSetRequests 1.3.6.1.2.1.11.27 </div><div class="line">snmpOutGetResponses 1.3.6.1.2.1.11.28 </div><div class="line">snmpOutTraps 1.3.6.1.2.1.11.29 </div><div class="line">snmpEnableAuthenTraps 1.3.6.1.2.1.11.30</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;SNMP抓包有结果后就可以在zabbix的配置页面上面开始配置添加路由器了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，添加一台主机，主机使用snmp协议</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8SNMP%20V3%E7%9B%91%E6%8E%A7%E8%B7%AF%E7%94%B1%E5%99%A8%E6%8E%A5%E5%8F%A3%E6%B5%81%E9%87%8F/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后创建一个应用集，名字叫做interfacetraffic</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8SNMP%20V3%E7%9B%91%E6%8E%A7%E8%B7%AF%E7%94%B1%E5%99%A8%E6%8E%A5%E5%8F%A3%E6%B5%81%E9%87%8F/02.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后在该应用集下面创建项目，其中键值和SNMP OID要一致，类型选择SNMPV3 代理程式，安全名称就是用户名，安全级别使用authpriv就是基于认证和加密的SNMPV3，我这里使用的是SHA认证，所以选中SHA，填写SHA密码，我这选的是AES加密密码，所以也要选中AES加密，并在下面输入密码，另外注意单位，我这使用的是bps，因为SNMP取到的是字节为单位，这里改成bps的单位时候需要将源数据乘8，这里选中的是使用自定倍数，值填写为8，最后还得注意储存量得改为差量（每秒速率），因为SNMP获取到的接口流量是累加的，所以获取到的值也是累加的，这里调成差量（每秒速率）将使用后一次取到的值减去前一次取到的值，然后除以中间相差的秒数，这才是真正的改时间段平均带宽。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8SNMP%20V3%E7%9B%91%E6%8E%A7%E8%B7%AF%E7%94%B1%E5%99%A8%E6%8E%A5%E5%8F%A3%E6%B5%81%E9%87%8F/03.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面只是创建好了一个接口的入口流量，按照这个模板修改，可以添加多个接口的出入口流量监控项。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建完监控项之后就可以生成图表了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面是生成图表的过程：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8SNMP%20V3%E7%9B%91%E6%8E%A7%E8%B7%AF%E7%94%B1%E5%99%A8%E6%8E%A5%E5%8F%A3%E6%B5%81%E9%87%8F/04.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样，就可以监控该路由器的G0/0的出入流量了，截图如下：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E4%BD%BF%E7%94%A8SNMP%20V3%E7%9B%91%E6%8E%A7%E8%B7%AF%E7%94%B1%E5%99%A8%E6%8E%A5%E5%8F%A3%E6%B5%81%E9%87%8F/05.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;至此，该教程文档完结，在配置过程中若碰到了一些其他问题可以百度或者谷歌查找资料解决</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/20. zabbix设置邮件报警/">zabbix设置邮件报警</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<h2 id="一、首先安装mailx组件并配置好能够通过三方邮箱发送邮件"><a href="#一、首先安装mailx组件并配置好能够通过三方邮箱发送邮件" class="headerlink" title="一、首先安装mailx组件并配置好能够通过三方邮箱发送邮件"></a>一、首先安装mailx组件并配置好能够通过三方邮箱发送邮件</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install mailx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑mailx的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /etc/mail.rc</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在最末尾添加如下信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> from=xxxxxx@qq.com smtp=smtp.qq.com</div><div class="line"><span class="built_in">set</span> smtp-auth-user=xxxxxx@qq.com smtp-auth-password=xxxxxx</div><div class="line"><span class="built_in">set</span> smtp-auth=login</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存退出后测试邮件是否能够正常发送出去</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> <span class="string">"zabbix test mail"</span> |mail -s <span class="string">"zabbix"</span> xxx@163.com</div></pre></td></tr></table></figure>
<h2 id="二、配置Zabbix服务端邮件报警"><a href="#二、配置Zabbix服务端邮件报警" class="headerlink" title="二、配置Zabbix服务端邮件报警"></a>二、配置Zabbix服务端邮件报警</h2><h3 id="1、打开Zabbix"><a href="#1、打开Zabbix" class="headerlink" title="1、打开Zabbix"></a>1、打开Zabbix</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;管理-示警媒介类型-创建媒体类型</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/01.jpeg?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/02.jpeg?raw=true" alt=""></figure></p>
<p>名称：Sendmail<br>类型：脚本<br>脚本名称：sendmail.sh<br>已启用：勾选<br>存档</p>
<h3 id="2、设置Zabbix用户报警邮箱地址"><a href="#2、设置Zabbix用户报警邮箱地址" class="headerlink" title="2、设置Zabbix用户报警邮箱地址"></a>2、设置Zabbix用户报警邮箱地址</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;管理-用户-Admin (Zabbix Administrator)</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/03.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;切换到示警媒介-添加</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/04.jpeg?raw=true" alt=""></figure></p>
<p>类型：Sendmail<br>收件人：xxx@163.com<br>其他默认即可，也可<br>以根据需要设置<br>状态：已启用<br>存档</p>
<h3 id="3、设置Zabbix触发报警的动作"><a href="#3、设置Zabbix触发报警的动作" class="headerlink" title="3、设置Zabbix触发报警的动作"></a>3、设置Zabbix触发报警的动作</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;组态-动作-创建动作</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/05.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意，下面的默认接收人那一栏中需要添加（默认接收人：）这个字段，否则将可能导致邮件中文乱码</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>默认接收人：默认接收人：</code>故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>默认信息：</code><br>告警主机:{HOSTNAME1}<br>告警时间:{EVENT.DATE} {EVENT.TIME}<br>告警等级:{TRIGGER.SEVERITY}<br>告警信息: {TRIGGER.NAME}<br>告警项目:{TRIGGER.KEY1}<br>问题详情:{ITEM.NAME}:{ITEM.VALUE}<br>当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}<br>事件ID:{EVENT.ID}<br>恢复信息：打钩    </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意，下面的恢复主旨那一栏中需要添加（恢复主旨：）这个字段，否则将可能导致邮件中文乱码</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>恢复主旨：恢复主旨：</code>恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>恢复信息：</code><br>告警主机:{HOSTNAME1}<br>告警时间:{EVENT.DATE} {EVENT.TIME}<br>告警等级:{TRIGGER.SEVERITY}<br>告警信息: {TRIGGER.NAME}<br>告警项目:{TRIGGER.KEY1}<br>问题详情:{ITEM.NAME}:{ITEM.VALUE}<br>当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}<br>事件ID:{EVENT.ID}<br>已启用：打钩</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/06.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;切换到操作选项</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E8%AE%BE%E7%BD%AE%E9%82%AE%E4%BB%B6%E6%8A%A5%E8%AD%A6/07.jpeg?raw=true" alt=""></figure></p>
<p>新的<br>操作类型：送出信息<br>送到用户：添加<br>默认信息：打钩<br>选择用户：Admin<br>选择<br>仅送到：Sendmail<br>存档    </p>
<h3 id="4、添加Zabbix服务端邮件发送脚本"><a href="#4、添加Zabbix服务端邮件发送脚本" class="headerlink" title="4、添加Zabbix服务端邮件发送脚本"></a>4、添加Zabbix服务端邮件发送脚本</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span>/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts <span class="comment">#进入脚本存放目录</span></div><div class="line">vi sendmail.sh <span class="comment">#编辑，添加以下代码</span></div><div class="line"></div><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#export LANG=zh_CN.UTF-8         #该命令能够解决发送的中文变成了乱码的问题</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$3</span>"</span> | mail -s <span class="string">"<span class="variable">$2</span>"</span><span class="variable">$1</span></div><div class="line">:wq! <span class="comment">#保存退出</span></div><div class="line">chown zabbix.zabbix /usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/sendmail.sh       <span class="comment">#设置脚本所有者为zabbix用户</span></div><div class="line">chmod +x /usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/sendmail.sh      <span class="comment">#设置脚本执行权限</span></div></pre></td></tr></table></figure>
<h3 id="5、设置zabbix-server服务调用脚本的目录"><a href="#5、设置zabbix-server服务调用脚本的目录" class="headerlink" title="5、设置zabbix_server服务调用脚本的目录"></a>5、设置zabbix_server服务调用脚本的目录</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim/usr/<span class="built_in">local</span>/zabbix-2.4.4/etc/zabbix_server.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入zabbix_server配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">AlertScriptsPath=/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到上面这一行，将脚本存放目录写到此处</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存并重启zabbix_server服务</p>
<h2 id="三、测试Zabbix报警"><a href="#三、测试Zabbix报警" class="headerlink" title="三、测试Zabbix报警"></a>三、测试Zabbix报警</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭Zabbix客户端服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service zabbix_agentd stop</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看xxx@163.com邮箱，会收到报警邮件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再开启Zabbix客户端服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">service zabbix_agentd start</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看xxx@163.com邮箱，会收到恢复邮件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用外部邮箱账号发送报警邮件设置完成。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;至此，Zabbix邮件报警设置完成。</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/3">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
